package com.liangzhong.modules.court.CourtMediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ModulePanelTypeConst;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CourtEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxyconstant.court.CourtProxyConstant;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.court.ICourtView;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import lz.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *球场 
	 * @author Administrator
	 * 
	 */	
	public class CourtMediator extends ModuleMediator
	{
		public static const MediatorName :String = "CourtMediator";
		public function CourtMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get view():IModuleView
		{
			return viewComponent as IModuleView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModule);
			AppEventDispatcher.getInstance().addEventListener(CourtEvent.CLEAR_UP_TIME,onClearUPTime);
			AppEventDispatcher.getInstance().addEventListener(CourtEvent.UP_COURT,onUpCourtHanlder);
		}
		
		/**
		 * 球场升级 
		 * @param event
		 * 
		 */
		protected function onUpCourtHanlder(event:CourtEvent):void
		{
			this.sendNotification(CourtProxyConstant.COURT_MEDIATORTOCOMMAND,null,CourtProxyConstant.COURT_UP);
		}
		
		/**
		 *清除冷却时间 
		 * @param event
		 * 
		 */
		protected function onClearUPTime(event:CourtEvent):void
		{
			this.sendNotification(CourtProxyConstant.COURT_MEDIATORTOCOMMAND,null,CourtProxyConstant.CLEAR_UP_TIME);
		}
		
		/**
		 *退出界面  
		 * @param event
		 * 
		 */
		protected function onRemoveModule(event:ModuleEvent):void
		{
//			removeMediator();
			
			facade.removeMediator(MediatorName);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModule);
			AppEventDispatcher.getInstance().removeEventListener(CourtEvent.CLEAR_UP_TIME,onClearUPTime);
			AppEventDispatcher.getInstance().removeEventListener(CourtEvent.UP_COURT,onUpCourtHanlder);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [NotificationConstants.COMMON_DROP_TIME,
				CourtProxyConstant.GET_COURT_UP,
				CourtProxyConstant.GET_COURT_INFO,
				NotificationConstants.MATCH_ENTER_SECOND_SCENE,
				NotificationConstants.MATCH_LEAVE_SECOND_SCENE];
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case NotificationConstants.COMMON_DROP_TIME:
					if(notification.getBody() == BuildingType.STADIUM)
					{
						AppData.getInstance().courtDataManger.courtInfoVo.pretime = 0;
//						view.clearTime(notification.getBody());
						Alert.showErrorTip("时间清除成功");
					}
					
					break;
				case CourtProxyConstant.GET_COURT_UP:
//					view.courtUp(notification.getBody());
					break;
				case CourtProxyConstant.GET_COURT_INFO:
//					if(AppData.getInstance().courtDataManger.courtInfoVo.pretime > 0)
//						
//					else
//						
					
					if(view == null && ResourceManager.instance.isContainResource(ResourceConst.COURT_SWF))
						showUi();
					break;
				case NotificationConstants.MATCH_ENTER_SECOND_SCENE://进入比赛二级界面
				{
					if(viewComponent && viewComponent.hasOwnProperty("visible"))
					{
						viewComponent["visible"] = false;
					}
					break;
				}
				case NotificationConstants.MATCH_LEAVE_SECOND_SCENE://离开比赛二级界面
				{
					if(viewComponent && viewComponent.hasOwnProperty("visible"))
					{
						viewComponent["visible"] = true;
					}
					break;
				}
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			
//			return;
			if(AppData.getInstance().courtDataManger.courtInfoVo == null)
				this.sendNotification(CourtProxyConstant.COURT_MEDIATORTOCOMMAND,null,CourtProxyConstant.COURT_INFO);
//			return;
			resName = ResourceConst.COURT_SWF;
			if(ResourceManager.instance.isContainResource(ResourceConst.COURT_SWF))
			{
				showUi();
				//@lake 2013-11-14 guide use
				AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MODULE_LOAD_RES_COMPLETE);
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).court[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			if(viewComponent == null && AppData.getInstance().courtDataManger.courtInfoVo != null)
				showUi();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.COURT_SWF,resList)
		}
		
		/**
		 * 
		 * 
		 */
		private function showUi():void
		{
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.court.CourtView");
			viewComponent = cView;
//			this.sendNotification(NotificationConstants.MAINVIEW_ADD_DISPLAYOBJECT,{type:ModulePanelTypeConst.WINEBAR,dispalyObj:view.bg});
			cView.initUI();
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			
//			super.removeMediator();
			
//			this.sendNotification(NotificationConstants.MAINVIEW_REMOVE_DISPLAYOBJECT);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			removeEventListeners();
			super.onRemove();
			(view as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}